package com.frank.towerrush.split;

import java.util.ArrayList;
import java.util.List;

/**
 * 路径类
 * 
 * @author Frank
 *
 */
public class Path {
    private List<Road> roads;

    public Path(List<Flag> flags) {
        roads = new ArrayList<Road>();
        if (flags.size() > 1) {
            for (int i = 1; i < flags.size(); i++) {
                Flag start = flags.get(i - 1);
                Flag end = flags.get(i);
                roads.add(new Road(start, end));
            }
        }
    }

    public Flag getStart() {
        return roads.get(0).getStart();
    }

    public Flag getEnd() {
        return roads.get(roads.size() - 1).getEnd();
    }

    /**
     * 将当前位置移往下一个位置
     * 
     * @param now
     *            当前位置
     * @param speed
     *            速度
     * @param whichWay
     *            当前路段
     * @return 是否到了下一个路段上
     */
    public boolean moveToNext(Flag now, int speed, int whichWay) {
        // 如果索引到了最后的位置 直接退出
        if (whichWay >= roadCount()) {
            return true;
        } else {
            // 得到当前路段的终点
            Road way = roads.get(whichWay);
            Flag end = way.getEnd();
            // 设置偏移量
            int s = -1;
            // 当前路段的方向
            int dir = way.getDir();
            // 将当前位置移动
            now.move(dir, speed);
            switch (dir) {
            case Road.UP:
                if (now.getY() <= end.getY()) {
                    s = end.getY() - now.getY();
                    return moveToNext(now, end, whichWay, s);
                }
                break;
            case Road.DOWN:
                if (now.getY() >= end.getY()) {
                    s = now.getY() - end.getY();
                    return moveToNext(now, end, whichWay, s);
                }
                break;
            case Road.RIGHT:
                // 如果走到尽头
                if (now.getX() >= end.getX()) {
                    // 超出的距离
                    s = now.getX() - end.getX();
                    // 前往下一路段
                    return moveToNext(now, end, whichWay, s);
                }
                break;
            case Road.LEFT:
                if (now.getX() <= end.getX()) {
                    s = end.getX() - now.getX();
                    return moveToNext(now, end, whichWay, s);
                }
                break;
            default:
                break;
            }
            return false;
        }
    }

    /**
     * 移动修正
     * 
     * @param now
     *            当前位置
     * @param end
     *            当前路段的终点
     * @param whichWay
     *            当前路段索引
     * @param s
     *            超过的距离
     * @return 是否前往下一个路段
     */
    private boolean moveToNext(Flag now, Flag end, int whichWay, int s) {
        now.moveToThere(end);
        moveToNext(now, s, whichWay + 1);
        return true;
    }

    /**
     * 获取当前路段
     * 
     * @param whichWay
     *            当前路段的索引
     * @return 当前路段
     */
    public Road getRoad(int whichWay) {
        return roads.get(whichWay);
    }

    /**
     * @return 路段的数量
     */
    public int roadCount() {
        return roads.size();
    }
}
